<?php

class ProfileController extends Controller
{
	public $layout='profile';
	private $_model;
	
	/**
	 * Displays the login page
	 */
	public function actionProfile()
	{
		$model = $this->loadModel();
		$this->render('profile',array('model'=>$model));
	}

	/*
	 * @个人详细信息
	 */	
	public function actionDetail() {
		$model = $this->loadModel();
		$_details = $this->renderPartial('detail',array('model'=>$model),true);
		$_detailsInfo['_html'] = $_details;
		echo CJSON::encode($_detailsInfo);
	}
	
	/*
	 * @
	 */	
	public function actionBasicEdit() {
		$model = $this->loadModel();
		$_BasicDetails = $this->renderPartial('_basicinfo',array('model'=>$model),true);
		$_BasicDetailsInfo['_html'] = $_BasicDetails;
		echo CJSON::encode($_BasicDetailsInfo);
	}

	/*
	 * @得到以主键为条件的MODEL
	 */
	public function loadModel($_self = TRUE)
	{
		if($this->_model===null)
		{
			if($_self)
				$_userId = Yii::app()->user->getId();
			else
				$_userId = $_GET['uid'];

			if(isset($_userId))
			{
				$this->_model = Profile::model()->findByPk($_userId);
			}
			if($this->_model===null)
			{
				echo '非法错误';
				exit;
			}
		}
		return $this->_model;
	}
	
	/*
	 * @修改详细信息
	 */
	public function actionUpdateProfile()
	{
		$model=$this->loadModel();
		if(isset($_POST['ProfileForm']))
		{
			$model->attributes = $_POST['ProfileForm'];
			if($model->validate())
			{
				$model->save();
				echo CJSON::encode(array('_result'=>'validate_sucess'));
			}
			else
			{
				//处理一下错误 信息
				$_errors = $model->getErrors();
				$_errshow = array();
				foreach ($_errors as $key=>$val)
					$_errshow[] = $val[0];
				echo CJSON::encode(array('_result'=>'validate_faild','_message'=>implode('、', $_errshow)));
			}
		}
		else
		{
			echo '错误';
			exit;
		}
	}
	
	/*
	 * @检查权限
	 */
	public function actionAllowAdd()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$model = $this->loadModel(false);
			echo CJSON::encode(array('_allow'=>$model->allowadd));
		}
		else
			echo '错误';
	}
	
	/*
	 * @显示验证页面
	 */
	public function actionFriendHtml()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$selfModel = Users::model()->findByPk(CHtml::encode($_GET['uid']));
			$answers = DTable::model('{{users_profile_answers}}')->findAll(array(
										'select'=>'id,question,answer',
										'condition'=>'uid=:uid ',
										'params'=>array(':uid'=>CHtml::encode($_GET['uid'])),
			));

			$this->renderPartial('/friend/_allowadd',array('model'=>$selfModel,'answers'=>$answers));
		}
		else
			echo '错误';
	}
	
	/*
	 * @提交验证
	 */
	public function actionAddFriendSubmit()
	{
		if(Yii::app()->request->isAjaxRequest)
		{
			$addlowaddtheanswer = CHtml::encode($_POST['addlowaddtheanswer']);
			if(empty($addlowaddtheanswer))
			{
				echo CJSON::encode(array('_code'=>"-2"));
				exit;
			}
			$answers = DTable::model('{{users_profile_answers}}')->findByPk(CHtml::encode($_POST['addlowaddtheanswerid']));
			if($answers === null)
			{
				echo CJSON::encode(array('_code'=>"-3"));
				exit;
			}
			if($answers->answer === $addlowaddtheanswer)
			{
				//留言内容
				$_content = CHtml::encode($_POST['addlowaddyanzheng']);
				$_content = empty($_content) ? '无' : $_content;
				//选择的问题和答案
				$_useranswers['question'] = $answers->question;
				$_useranswers['answer'] = $answers->answer;
				$_other = array('_content'=>$_content,'_answer'=>$_useranswers);

				$this->InsertNotice(Yii::app()->user->getId(), $answers->uid, 'addfriendrequest',$this->_userProfileModel->nickname.' 想加你为熟人。',CJSON::encode($_other));
				$info['_code'] = "1";
			}
			else
				$info['_code'] = "-1";

			echo CJSON::encode($info);
		}
		else
			echo '错误';
	}
	
	public function InsertNotice($from_id, $to_id,$notice_type ,$content, $other)
	{
		//发送通知
		$_noticemodel = new DTable('{{users_notice}}');
		$_noticemodel->from_id = $from_id;
		$_noticemodel->to_id = $to_id;
		$_noticemodel->notice_type = $notice_type;
		$_noticemodel->content = $content;
		$_noticemodel->other = $other;
		$_noticemodel->createdate = date('Y-m-d H:i:s');
		$_noticemodel->save();
	}
}